package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private AppointmentMapper service;
    @Autowired
    private StatementItemMapper statementItemService;
    @Override
    public void save(Statement statement) {
        Statement obj=statement;
        obj.setCustomerName(statement.getCustomerName());
        obj.setCustomerPhone(statement.getCustomerPhone());
        obj.setActualArrivalTime(statement.getActualArrivalTime());
        obj.setLicensePlate(statement.getLicensePlate());
        obj.setCarSeries(statement.getCarSeries());
        obj.setServiceType(statement.getServiceType());
        obj.setInfo(statement.getInfo());
        obj.setCreateTime(new Date());
        statementMapper.insert(obj);
    }

    @Override
    public void update(Statement statement) {
        Statement obj=this.get(statement.getId());
        if (Statement.STATUS_PAID.equals(obj.getStatus())){
            throw new BusinessException("已支付不能修改");
        }
        statementMapper.updateByPrimaryKey(obj);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        Statement statement;
        for (Long dictId : dictIds) {
            statement=this.get(dictId);
            if (Statement.STATUS_PAID.equals(statement.getStatus())){
                throw new BusinessException("以支付不能删除");
            }
             statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }
    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
//        if (qo.getName()!=null && qo.getName()!="") {
//            qo.setName(qo.getName().substring(0, qo.getName().lastIndexOf(",")));
//        }
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<Statement> statements = statementMapper.selectForList(qo);
        return new TablePageInfo<Statement>(statements);
    }

    @Override
    public void updateStatement(Statement statement) {
        statementMapper.updateStatement(statement);
    }

    @Override
    public void pay(Long statementId) {
        Statement statement = this.get(statementId);
        if (statement.getServiceType().equals(Statement.STATUS_PAID)){
            throw new BusinessException("已经支付完毕不要重复提交");
        }
        if (statement.getAppointmentId()!=null){
            service.cancelOp(statement.getAppointmentId(), Appointment.STATUS_PAID);
        }
        Long id=ShiroUtils.getUserId();
        Date time=new Date();
        statementMapper.pay(statementId,Statement.STATUS_PAID,id,time);
    }

    @Override
    public Statement getByAppointmentId(Long appointmentId) {
       return statementMapper.getByAppointmentId(appointmentId);
    }

    @Override
    public Workbook export(Long statementId) {
        Workbook wk= new XSSFWorkbook();
        Sheet sheet = wk.createSheet("明细账单");
        Statement statement = this.get(statementId);
        Row row = sheet.createRow(0);
        Row row2 = sheet.createRow(1);
        Row row3 = sheet.createRow(2);
        Row row4 = sheet.createRow(3);
        Row row5 = sheet.createRow(4);

        row.createCell(0).setCellValue("客户姓名:");
        row.createCell(1).setCellValue(statement.getCustomerName());
        row.createCell(3).setCellValue("联系方式:");
        row.createCell(4).setCellValue(statement.getCustomerPhone());

        row2.createCell(0).setCellValue("车牌号码:");
        row2.createCell(1).setCellValue(statement.getLicensePlate());
        row2.createCell(3).setCellValue("汽车类型:");
        row2.createCell(4).setCellValue(statement.getCarSeries());

        row3.createCell(0).setCellValue("服务类型:");
        row3.createCell(1).setCellValue(statement.getServiceType());
        row3.createCell(3).setCellValue("到店时间:");
        row3.createCell(4).setCellValue(statement.getActualArrivalTime());

        row4.createCell(0).setCellValue("总消费金额:");
        row4.createCell(1).setCellValue(statement.getTotalAmount().toString());
        row4.createCell(3).setCellValue("实付价格");
        row4.createCell(4).setCellValue(statement.getTotalAmount().subtract(statement.getDiscountAmount()).toString());

        row5.createCell(0).setCellValue("优惠价格");
        row5.createCell(1).setCellValue(statement.getDiscountAmount().toString());

        List<StatementItem> statementItems = statementItemService.queryByStatementId(statementId);
        Row row6 = sheet.createRow(6);
        row6.createCell(2).setCellValue("服务项目");
        Row row7 = sheet.createRow(7);
        row7.createCell(0).setCellValue("服务名称");
        row7.createCell(2).setCellValue("服务价格");
        row7.createCell(4).setCellValue("数量");
        for (int i = 0; i < statementItems.size(); i++) {
            Row row1 = sheet.createRow(i+8);
            row1.createCell(0).setCellValue(statementItems.get(i).getItemName());
            row1.createCell(2).setCellValue(statementItems.get(i).getItemPrice().toString());
            row1.createCell(4).setCellValue(statementItems.get(i).getItemQuantity().toString());
        }
        return wk;
    }

}
